package com.ease.integration.converter;

import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xml.sax.InputSource;

import com.ease.integration.definition.constant.IntegrationMessageTemplateReserveKey;

import freemarker.ext.dom.NodeModel;

/**
 * 把系统间的参数转换为内部java对象能直接映射的xml
 * 
 * @author nathanleewei
 * 
 */
public abstract class ArgumentXmlFormatConverter {

	private static final Log logger = LogFactory.getLog(ArgumentXmlFormatConverter.class);

	/**
	 * 把调用参数(string,int,date,xml)转为bean的xml表示
	 * 
	 * @param parameters
	 * @param template
	 * @return
	 */
	public static String format(Map<String, Object> parameters, String template) {
		NodeModel model = null;
		String formattedXml = null;

		try {
			Map<String, Object> tmpMap = new HashMap<String, Object>();
			for (String key : parameters.keySet()) {
				if (key.startsWith(IntegrationMessageTemplateReserveKey.XML_STRING)) {
					String xml = (String) parameters.get(key);
					model = NodeModel.parse(new InputSource(new StringReader(xml)));
					tmpMap.put(key.substring(11) + IntegrationMessageTemplateReserveKey.XML_MODEL, model);
				}
			}
			parameters.putAll(tmpMap);

			// formattedXml = FreeMarkerSupport.getInstance().processPath(template, parameters);
		} catch (Exception e) {
			logger.error(e, e);
		}
		return formattedXml;
	}

}
